#include "StdAfx.h"
#include "MainFramUI.h"
#include "SoftStoreUI.h"
#include "../resource.h"
#include "UnIstallUI.h"
#include "SoftUpdateUI.h"
#include "DownloadUI.h"

#include "F:\\QQSoftMgr\\DuiLib\\Control\\UIMenu.h"


CMainFramUI::CMainFramUI(void):m_pSoftWondiw(NULL),m_pUnInstallWindow(NULL)
,m_pSoftUpdateWindow(NULL)
,m_pDownLoadWindow(NULL)
{
}

CMainFramUI::~CMainFramUI(void)
{
}

LRESULT CMainFramUI::OnCreate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
	LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
	styleValue &= ~WS_CAPTION;
	styleValue &= ~WS_THICKFRAME;
	::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
	m_pm.Init(m_hWnd);
	CDialogBuilder builder;
	CControlUI* pRoot = builder.Create(_T("skin.xml"), (UINT)0,NULL, &m_pm);
	ASSERT(pRoot && "Failed to parse XML");
	m_pm.AttachDialog(pRoot);
	m_pm.AddNotifier(this);
	SetIcon(IDI_QQSOFTMGR);
	CenterWindow();
	CChildWindowUI * m_pChildWnd  = static_cast<CChildWindowUI*>(m_pm.FindControl(_T("SoftStoreWnd")));
	if(m_pChildWnd != NULL)
	{	
		m_pSoftWondiw = new CSoftStoreWnd(m_pChildWnd->GetXml());
		m_pSoftWondiw->Create(m_hWnd, NULL, 
			UI_WNDSTYLE_CHILD,
			NULL, 0, 0, 300, 500);
		m_pChildWnd->SetWindow(m_pSoftWondiw);
	}
	return 1;
}

LPCTSTR CMainFramUI::GetWindowClassName() const
{
	return _T("QQSoftMgr");
}

LRESULT CMainFramUI::HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	LRESULT lRes = 0;
	BOOL bHandled = TRUE;
	switch( uMsg ) 
	{
		case WM_CREATE:        lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
		case WM_NCHITTEST:     lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;
		case WM_NCLBUTTONDBLCLK:      OnNcLButtonDblClk(wParam,lParam,bHandled);break;
		case WM_SYSCOMMAND:    lRes = OnSysCommand(uMsg, wParam, lParam, bHandled); break;
		default:
			bHandled = FALSE;
	}

	if( bHandled ) return lRes;
	if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;
	return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
}

LRESULT CMainFramUI::OnNcHitTest( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
	POINT pt; 
	pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
	::ScreenToClient(*this, &pt);
	RECT rcClient;
	::GetClientRect(*this, &rcClient);
	RECT rcCaption = m_pm.GetCaptionRect();
	if( pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
		&& pt.y >= rcCaption.top && pt.y < rcCaption.bottom ) 
	{
		CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
		if( pControl && _tcscmp(pControl->GetClass(), _T("ButtonUI")) != 0 && 
			_tcscmp(pControl->GetClass(), _T("OptionUI")) != 0 &&
			_tcscmp(pControl->GetClass(), _T("EditUI")) != 0
			)
			return HTCAPTION;
	}
	return HTCLIENT;
}

void CMainFramUI::Notify( TNotifyUI& msg )
{	
	if( msg.sType == _T("click") ) 
	{
		if( msg.pSender->GetName() == _T("closebtn") ) 
		{
			PostQuitMessage(0);
			return; 
		}
		else if (msg.pSender->GetName()==_T("minbtn"))
		{
			SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
			return; 
		}
		else if( msg.pSender->GetName() == _T("menubtn"))
		{
			CDUIMenu * pMenu  = static_cast<CDUIMenu*>(m_pm.FindPopMenu(_T("MyMenu")));
			POINT pt;
			RECT rtClient;
			::GetClientRect(m_pm.GetPaintWindow(),&rtClient);
			::ClientToScreen(m_pm.GetPaintWindow(), (LPPOINT)&rtClient);
			::ClientToScreen(m_pm.GetPaintWindow(), ((LPPOINT)&rtClient + 1));
			RECT rtControl = msg.pSender->GetPos();
			pt.x = rtClient.left + rtControl.left-50;
			pt.y = rtClient.top + rtControl.bottom;

			pMenu->TrackPopupMenu(&m_pm,pt);
			return; 
		}

	}
	else if(msg.sType==_T("selectchanged"))
	{
		CDuiString name = msg.pSender->GetName();
		CTabLayoutUI* pControl = static_cast<CTabLayoutUI*>(m_pm.FindControl(_T("switch")));
		if(name==_T("SoftStore"))
			pControl->SelectItem(0);
		if(name==_T("SoftUpdate"))
		{
			if (m_pSoftUpdateWindow == NULL)
			{
				CChildWindowUI * m_pChildWnd  = static_cast<CChildWindowUI*>(m_pm.FindControl(_T("SoftUpdateWnd")));
				if(m_pChildWnd != NULL)
				{	
					m_pSoftUpdateWindow = new CSoftUpdateWnd(m_pChildWnd->GetXml());
					m_pSoftUpdateWindow->Create(m_hWnd, NULL, 
						UI_WNDSTYLE_CHILD,
						NULL, 0, 0, 300, 500);
					m_pChildWnd->SetWindow(m_pSoftUpdateWindow);
				}
			}
			pControl->SelectItem(1);
		}
		else if(name==_T("Unintall"))
		{
			if (m_pUnInstallWindow == NULL)
			{
				CChildWindowUI *m_pChildWnd  = static_cast<CChildWindowUI*>(m_pm.FindControl(_T("UnistallWnd")));
				if(m_pChildWnd != NULL)
				{	
					m_pUnInstallWindow = new CUnIstallWnd(m_pChildWnd->GetXml());
					m_pUnInstallWindow->Create(m_hWnd, NULL, 
						UI_WNDSTYLE_CHILD,
						NULL, 0, 0, 300, 500);
					m_pChildWnd->SetWindow(m_pUnInstallWindow);
				}
			}
			pControl->SelectItem(2);
		}
		else if (name ==_T("AndroidGame"))
		{
			pControl->SelectItem(3);
		}
		else if (name == _T("download"))
		{
			if (m_pDownLoadWindow == NULL)
			{
				CChildWindowUI *m_pChildWnd  = static_cast<CChildWindowUI*>(m_pm.FindControl(_T("DownLoadWnd")));
				if(m_pChildWnd != NULL)
				{	
					m_pDownLoadWindow = new CDownLoadWnd(m_pChildWnd->GetXml());
					m_pDownLoadWindow->Create(m_hWnd, NULL, 
						UI_WNDSTYLE_CHILD,
						NULL, 0, 0, 300, 500);
					m_pChildWnd->SetWindow(m_pDownLoadWindow);
				}
			}
			pControl->SelectItem(4);
		}
		if (name == _T("AndroidGames"))
		{
			CWebBrowserUI *pWeb = static_cast<CWebBrowserUI*>(m_pm.FindControl(_T("browser")));
			if (pWeb != NULL)
			{
				pWeb->Navigate2(_T("http://s.pcmgr.qq.com/sy/index.html?version=8.5.10251.228&guid=3eb16c33beca8c1e2f544364485e6a29&from=0"));
			}
		}
		else if (name == _T("3366games"))
		{
			CWebBrowserUI *pWeb  = static_cast<CWebBrowserUI*>(m_pm.FindControl(_T("browser")));
			if (pWeb != NULL)
			{
				pWeb->Navigate2(_T("http://s.pcmgr.qq.com/sy/index.html?version=8.5.10251.228&guid=3eb16c33beca8c1e2f544364485e6a29&from=0"));
			}
		}
		else if (name == _T("webgames"))
		{
			CWebBrowserUI *pWeb  = static_cast<CWebBrowserUI*>(m_pm.FindControl(_T("browser")));
			if (pWeb != NULL)
			{
				pWeb->Navigate2(_T("http://s.pcmgr.qq.com/game/index-7.html?newlogin=1&version=8.5.10251.228&guid=3eb16c33beca8c1e2f544364485e6a29&keyindex=18"));
			}
		}
	}
}

LRESULT CMainFramUI::OnSysCommand( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
	if( SC_CLOSE == wParam ) 
	{
		::PostQuitMessage(0L);
		bHandled = TRUE;
		return 0;
	}
	bHandled = FALSE;
}

void  CMainFramUI::OnNcLButtonDblClk( WPARAM wParam,LPARAM lParam,BOOL &bHandled )
{
	if(HTCAPTION == wParam)
	{
		bHandled = TRUE;
		return;
	}
	bHandled = FALSE;
}
